var paytype = 1,w = null,pays = {},cur_page;
var app = angular.module("app", []);
app.controller("my_controller", function($scope, $http) {
	$scope.intview = function() {
		$http({
			method: "post",
			url: apiRoot,
			data: {
				action: "Orderid.getOrderInfo",
				ddid: orderId
			}
		}).then(function(result) {
			$scope.res = result.data.data; 
			console.log(JSON.stringify($scope.res.order));  
			$scope.state = state;
			$scope.status = status;
		}, function errorfunction(e) {
			console.log(JSON.stringify(e)); 
		})
	}

	$scope.ordershen = function($id, $state) {
		console.log($id+"####################"+ $state);
		$http({
			method: 'post',
			url: apiRoot,
			data: {
				action: "Orderid.orderidstate",
				id: $id,
				state: $state 
			}
		}).then(function successCallback(data) {
			console.log(JSON.stringify(data));
			if(data.data.error == 0) {
				plus.nativeUI.toast('提交成功');   
				cur_page = (cur_page > 1) ? (cur_page - 1) : 1;
			  	console.log(cur_page);
			  	$scope.initView();
			} else {
				plus.nativeUI.toast('提交失败');
			}
		}, function errorfunction(e) {
			plus.nativeUI.toast('提交失败');
		})
	}

	//评论
	$scope.pinglun = function($id) {
		openPage('evaluate_for_goods.html', { ddid: $id })
	}
	//付款
//	$scope.pay = function($id, $num, $type, $ddid) {
//		openPage('confirm_order.html', { goodId: $id, goodsNum: $num, pay_type: $type, orderId: $ddid });
//	}

	$scope.evaluate = function(ddid) {
		openPage('evaluate_for_goods.html', { ddid: ddid });
	}
	
	
	 
	/*调起支付*/
$scope.canpay=function(money,orderid,trade_name) {
//	alert(money+"#####"+orderid+"#####"+trade_name);
	if(w) {
		return;
	} //检查是否请求订单中
	if(paytype == 0) {
		ptype = 'alipay';
		url = webRoot + '/pay/Alipay.php?orderid=' + orderid + '&price=' + money;
	} else if(paytype == 1) {
		ptype = 'wxpay';
		url = webRoot + '/pay/index.php?price=' + money + '&orderid=' + orderid + '&trade_name=' + trade_name;
	}

	w = plus.nativeUI.showWaiting('支付中...', {
		width: '80px',
		height: '80px',
		background: 'rgba(0,0,0,0.3)'
	});
	// 请求支付订单
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		switch(xhr.readyState) {
			case 4:
				w.close();
				w = null;
				if(xhr.status == 200) {
					var order = xhr.responseText;
//					alert(order);
					plus.payment.request(pays[ptype], order, function(result) {
						toast('支付成功');
						//setSmallchange();/*保存记录，余额不变*/
					}, function(e){
						console.log("[" + e.code + "]：" + e.message);
					});
				} else {
					plus.nativeUI.toast("获取订单信息失败！");
				}
				break;
			default:
				break;
		}
	}
	xhr.open('get', url);
	xhr.send(); 
}
	
	
})
var ws, orderId, cur_state, status;
document.addEventListener("plusready", function() {
	ws = plus.webview.currentWebview();
	orderId = ws.orderId;
	state = ws.cur_state;
	status = ws.cur_status;
	appElement = document.querySelector('[ng-controller=my_controller]');
	$scope = angular.element(appElement).scope();
	$scope.intview();
	$scope.$apply();
			
			
			
	// 获取支付通道
	plus.payment.getChannels(function(channels) {
		for(var i in channels) {
			var channel = channels[i];
			pays[channel.id] = channel;
			checkServices(channel);
		}
	}, function(e) {
		toast("获取支付通道失败：" + e.message);
		outLine("获取支付通道失败：" + e.message);
	});

	// 检测是否安装支付服务 
	function checkServices(pc){
		if(!pc.serviceReady) {
			var txt = null;
			switch(pc.id) {
//				case "alipay":
//					txt = "检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
//					break;
				default:
					txt = "系统未安装“" + pc.description + "”服务，无法完成支付，是否立即安装？";
					break;
			}
			plus.nativeUI.confirm(txt, function(e) {
				if(e.index == 0) {
					pc.installService();
				}
			}, pc.description);
		}
	}
})